﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using SO_DataFetcher.PubFunctions.DataBaseHelp;

namespace SO_DataFetcher.PubFunctions.DataLink
{
    public class PubDataLinks
    {

        public static Dictionary<string, DataLinkStruct> DataLinks = new Dictionary<string, DataLinkStruct>();    //公共字典集
        public static DataSet  DatasetLinks = null;    //公共数据集
        public static DataTable DataTableDataLinks = new DataTable();
        private static readonly string XmlPath = PubCommon.XmlDataLinksPath;

        private static bool _isXmlExist = false;

        /// <summary>
        /// 初始化数据连接
        /// </summary>
        public static void Init()
        {
            try
            {
                IsXmlExist();

                if (_isXmlExist)
                    GetTableDataLinks();
                else
                {
                    DataTableDataLinks = new DataTable();
                    DataTableDataLinks.Columns.Add("Index", typeof (string));
                    DataTableDataLinks.Columns.Add("Id", typeof (string));
                    DataTableDataLinks.Columns.Add("LinkType", typeof (string));
                    DataTableDataLinks.Columns.Add("Ip", typeof (string));
                    DataTableDataLinks.Columns.Add("Port", typeof (string));
                    DataTableDataLinks.Columns.Add("Name", typeof (string));
                    DataTableDataLinks.Columns.Add("Password", typeof (string));
                    DataTableDataLinks.Columns.Add("DataTable", typeof (string));
                    DataTableDataLinks.Columns.Add("Description", typeof (string));
                }

                PubLog.WriteLog("初始化数据连接配置文件完成");
            }
            catch (Exception ex)
            {
                PubLog.WriteLog("初始化数据连接配置文件错误：" + ex.Message);
            }
        }

        public static void IsXmlExist()
        {
            _isXmlExist = File.Exists(XmlPath);
        }

        /// <summary>
        /// 更新连接数据
        /// </summary>
        public static void GetTableDataLinks()
        {
            DatasetLinks = PubXml.GetDataSetFromXml(XmlPath);

            if (DatasetLinks.Tables.Count > 0)
            {
                DataTableDataLinks = DatasetLinks.Tables[0];

                if (DataTableDataLinks != null && DataTableDataLinks.Rows.Count > 0)
                    GetDictionaryDataLinks();
            }
        }


        /// <summary>
        /// 存储为字典变量
        /// </summary>
        private static void GetDictionaryDataLinks()
        {
            DataLinks = new Dictionary<string, DataLinkStruct>();

            foreach (DataRow dataRow in DataTableDataLinks.Rows)
            {  
                string index = dataRow["Index"].ToString();
                string id = dataRow["Id"].ToString();
                string type = dataRow["LinkType"].ToString();
                string ip = dataRow["Ip"].ToString();
                string port = dataRow["Port"].ToString();
                string name = dataRow["Name"].ToString();
                string psw = dataRow["Password"].ToString();
                string datatable = dataRow["DataTable"].ToString();
                string desc = dataRow["Description"].ToString();
                              
                var dataLinkStruct = new DataLinkStruct
                {
                    Index = index,
                    Id = id,
                    LinkType = type,
                    Ip = ip,
                    Port = port,
                    Name = name,
                    Password = psw,
                    DataTable = datatable,
                    Description = desc
                };

                DataLinks.Add(id,dataLinkStruct);
            }
        }


        /// <summary>
        /// 保存修改后的dataset到xml文件中
        /// </summary>
        public static void SaveDataSetLinks()
        {
            if (DatasetLinks != null)
            {
                DatasetLinks.Tables.Clear();
                DatasetLinks.Tables.Add(DataTableDataLinks);
                GetDictionaryDataLinks();
                XmlUtility.SaveXmlSetting(DatasetLinks, XmlPath);
            }
        }


        public static DataTable GetCurrentActionTable(string[] sqls, string linkId) 
        {
            DataTable dataTable = new DataTable();

            if (DataLinks.ContainsKey(linkId))
            {
                DataLinkStruct dataLink = DataLinks[linkId];

                DataBaseHelper dataBaseHelper = null;

                switch (dataLink.LinkType)
                {
                    case "Oracle":
                        dataBaseHelper =new OracleHelp();
                        //OracleHelp.Instance.Init(dataLink.Ip,dataLink.Port,dataLink.Name,dataLink.Password,dataLink.DataTable);
                        //dataTable = OracleHelp.Instance.ExecuteSqls(sqls);
                        break;
                    case "SQLServer":
                        dataBaseHelper =new SqlServerHelp();
                         //SqlServerHelp.Instance.Init(dataLink.Ip,dataLink.Port,dataLink.Name,dataLink.Password,dataLink.DataTable);
                         //dataTable = SqlServerHelp.Instance.ExecuteSqls(sqls); 
                        break;
                    case "MySQL":
                        dataBaseHelper = new MySqlHelp();
                        //MySqlHelp.Instance.Init(dataLink.Ip, dataLink.Port, dataLink.Name, dataLink.Password, dataLink.DataTable);
                        //dataTable = MySqlHelp.Instance.ExecuteSqls(sqls); 
                        break;
                }

                if (dataBaseHelper != null)
                {
                    dataBaseHelper.Instance();
                    dataTable = dataBaseHelper.ExecuteSqls(sqls);
                }
            }
            return dataTable;
        }

    }
}
